/* ADR2TXT.EXE  Converts CIM address book into plain ASCII text.

   (c) 1992 David J. Lindbergh
   
   Permission is hereby granted for unrestricted copying, use, and
   modification, so long as author credit and this permission
   statement are included.

   Rev 1                                                 1992-07-01 DJL
   Rev 2  Renamed to from DAT2TXT to ADR2TXT
          Updated to read DOSCIM 2.1 / WINCIM 1.0 file format,
            as well as old DOSCIM 1.x format.
            Note that this program CANNOT currently handle "groups".
          Default con: output added
                                                         1993-01-25 DJL                    
*/

#include <dos.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>

typedef unsigned char byte;
typedef unsigned int  word;

/**********************************************************************/

void main(int argc, char *argv[])
{
	int i, len;

	FILE *in, *out;   /* file pointers */

	char *inputfile	= argv[1];
	char *outputfile	= argv[2];

	if (argc < 2)
   {
      printf("Syntax: ADR2TXT input [output.txt]\n");
      printf("(c) 1993-01-25 David Lindbergh");
      exit(1);
   }

   if (argc < 3)     /* if only input file specfied, default to con: output */
      outputfile = "con";


	if ((in = fopen(inputfile,"rb")) == NULL)
	{
		printf("ERROR: Unable to open input file %s\n",inputfile);
		exit(1);
	}
	setvbuf(in, NULL, _IOFBF, 30 * 1024);

	if ((out = fopen(outputfile,"wb")) == NULL)
	{
		printf("ERROR: Unable to open output file %s\n",outputfile);
		exit(1);
	}
	setvbuf(out, NULL, _IOFBF, 30 * 1024);


   /*******************/
   /* process records */
   /*******************/

   switch(getc(in))                       /* read version code */
   {
      case 0x01:                          /* version code = CIM 1.x */
   
         fprintf(out, "%d address book entries:\n\r\n\r", getc(in));

	      while ((len = getc(in)) != EOF)     /* read record length */
         {
            for (i = 0; i < len; i++)        /* copy to output */
               putc(getc(in), out);

		      fprintf(out, "\r\n");
         }

         break;

      case 0x02:                          /* DOSCIM 2.1+, WINCIM 1.x */
      {
         int num_entries, count;
         unsigned int descriptor;

         num_entries = getw(in);

         fprintf(out, "%d address book entries:\n\r\n\r", num_entries);

         for (count = 0; count < num_entries; count++)
         {
            descriptor = getw(in);

            for (i = getc(in); i > 0; i--)   /* copy name to output */
               putc(getc(in), out);

		      fprintf(out, "\r\n");

            if ((descriptor & 0x8000) != 0)  /* if GROUP */
            {
               for (i = getc(in); i > 0; i--)   /* read & ignore group info */
                  getw(in);
            }
            else
            {
               for (i = getc(in); i > 0; i--)   /* copy address to output */
                  putc(getc(in), out);

		         fprintf(out, "\r\n");
            }

            for (i = getc(in); i > 0; i--)      /* copy comment to output */
               putc(getc(in), out);

		      fprintf(out, "\r\n");
         }

         break;
      }

      default:

         printf("Unknown file format.  Aborting.\n");
   }
}

